package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.*;
import com.dingyh.shopwebbackground.service.*;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-22
 */
@RestController
@CrossOrigin
@RequestMapping("/ads-post")
public class AdsPostController {

    @Autowired
    AdsPostService adsPostService;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private PayoutsService payoutsService;

    @Autowired
    UserService userService;

    @Autowired
    BusinessService businessService;

    @RequestMapping("getAdsList")
    public CommonResult<Object> getAdsList(@RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "6",required = true )int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<AdsPost> adsPosts = adsPostService.getAdsList();
        PageInfo<AdsPost> pageInfo =new PageInfo<>(adsPosts);
        return new CommonResult(200,"",pageInfo);
    }

    @RequestMapping("/approvalAds")
    public CommonResult<Object> approvalAds(AdsPost adsPost){
        Commodity commodity = commodityService.getById(adsPost.getGoodsId());
//        if(!commodity.getIspush().equals("0")){
//            return new CommonResult<>(500,"该商品的广告投放活动仍在有效期",null);
//        }
        Payouts payouts = payoutsService.getById(adsPost.getPayId());
        payouts.setState("3-2");
        payouts.setTimearrival(System.currentTimeMillis());
        payoutsService.updateById(payouts);
        commodity.setIspush(adsPost.getWherePost());
        commodityService.updateById(commodity);
        User user = userService.getById(commodity.getCompanyid());
        user.setMoney(user.getMoney().subtract(adsPost.getMoney()));
        userService.updateById(user);
        if(adsPostService.approvalAds(adsPost)){
            return new CommonResult<>(200,"审批成功！","");
        }else {
            return new CommonResult<>(500,"审批失败，请稍后重试！",null);
        }
    }


    @RequestMapping("/addAds")
    public CommonResult<Object> addAds(String saleId, Long timeStart, Long timeEnd, BigDecimal money, String wherePost, String goodsId){
        String current = UUIDUtil.generator();
        AdsPost adsPost = AdsPost.builder()
                .id(UUIDUtil.generator())
                .saleId(saleId)
                .ifApproval("0")
                .timeStart(timeStart)
                .timeEnd(timeEnd)
                .money(money)
                .wherePost(wherePost)
                .goodsId(goodsId)
                .payId(current)
                .build();
        Payouts payouts = Payouts.builder()
                .id(current)
                .source("3")
                .timeadd(System.currentTimeMillis())
                .state("3-1")
                .money(adsPost.getMoney())
                .build();
        payoutsService.save(payouts);
        if (adsPostService.save(adsPost)){
            return new CommonResult(200, "提交成功！", null);
        } else {
            return new CommonResult(500, "提交失败！", null);
        }
    }

    @RequestMapping("/returnAds")
    public CommonResult<Object> returnAds(AdsPost adsPost){
        if (adsPostService.approvalAds(adsPost)) {
            return new CommonResult<>(200,"退回成功！",null);
        }else {
            return new CommonResult<>(500,"退回失败！",null);
        }
    }

}

